package com.onedec.menu.controller;

import com.onedec.menu.entity.*;
import com.onedec.menu.service.*;
import com.onedec.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptPermissionService deptPermissionService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @GetMapping("getMenu/{id}")
    public void getById(@PathVariable("id") int id) {
        try {
            System.out.println(menuService.selectById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getMenus")
    public ResponseEntity getMenu(Integer eid) throws Exception {
        List<OnedecPermission> list = menuService.getMenus(2);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("获取菜单=" + list.get(i));
        }
        return new ResponseEntity("200", "success", list);
    }

    @RequestMapping("getAllSubmenus")
    public ResponseEntity getAllSubmenus() throws Exception{
        List<OnedecPermission> list=  menuService.getAllSubMenus();
        return new ResponseEntity("200","success",list);
    }

    @PostMapping("addMenu")
    @Transactional
    public ResponseEntity addMenu(@RequestBody List<Menu> list,int deptId) throws Exception{
        List<OnedecDeptPermission> permissions = deptPermissionService.selectPermissionByDeptid(deptId);

        deptPermissionService.delete(deptId);
//        //必须使用迭代器边循环边删除
//        for (OnedecDeptPermission permission:permissions) {
//            Iterator<Menu> iterator = list.iterator();
//            while (iterator.hasNext()){
//                Menu next = iterator.next();
//                if(next.getValue()==permission.getPermissionId()){
//                    iterator.remove();
//                }
//            }
//        }

        //得到差集
        System.out.println(list.toString());
        for (Menu menu:list) {
            OnedecDeptPermission onedecDeptPermission = new OnedecDeptPermission();
            onedecDeptPermission.setDeptId(deptId);
            OnedecPermission onedecPermission = menuService.selectById(menu.getValue());
            if(onedecPermission.getPermissionLevel()==1){
                onedecDeptPermission.setPermissionId(menu.getValue());
                deptPermissionService.insert(onedecDeptPermission);
            }else if(onedecPermission.getPermissionLevel()==2){
                int parentId = onedecPermission.getPermissionParentId();
                System.out.println(parentId);
                onedecDeptPermission.setPermissionId(menu.getValue());
                deptPermissionService.insert(onedecDeptPermission);

                if(deptPermissionService.selectByDeptIdAndPermissionId(deptId,parentId).size()==0){
                    System.out.println("111");
                    onedecDeptPermission.setPermissionId(parentId);
                    deptPermissionService.insert(onedecDeptPermission);
                }
            }

        }
        return new ResponseEntity("200","success",null);
    }

    @PostMapping("addRoleMenu")
    @Transactional
    public ResponseEntity addRoleMenu(@RequestBody List<Menu> list,int roleId) throws Exception{
        List<OnedecRolePermission> permissions = rolePermissionService.selectPermissionByRoleid(roleId);
        rolePermissionService.delete(roleId);
        //得到差集
        for (Menu menu:list) {
            OnedecRolePermission onedecRolePermission = new OnedecRolePermission();
            onedecRolePermission.setRoleId(roleId);
            OnedecPermission onedecPermission = menuService.selectById(menu.getValue());
            if(onedecPermission.getPermissionLevel()==1){
                onedecRolePermission.setPermissionId(menu.getValue());
                rolePermissionService.insert(onedecRolePermission);
            }else if(onedecPermission.getPermissionLevel()==2){
                int parentId = onedecPermission.getPermissionParentId();
                System.out.println(parentId);
                onedecRolePermission.setPermissionId(menu.getValue());
                rolePermissionService.insert(onedecRolePermission);

                if(rolePermissionService.selectByRoleIdAndPermissionId(roleId,parentId).size()==0){

                    onedecRolePermission.setPermissionId(parentId);
                    rolePermissionService.insert(onedecRolePermission);
                }
            }

        }
        return new ResponseEntity("200","success",null);
    }


    @RequestMapping("getAllDepts")
    public ResponseEntity getAllDepts( )throws Exception{
        return new ResponseEntity("200","success",deptService.selectAll());
    }

    @RequestMapping("getAllRoles")
    public ResponseEntity getAllRoles( )throws Exception{
        return new ResponseEntity("200","success",roleService.selectAll());
    }


    @RequestMapping("getPermissionsById")
    public ResponseEntity getPermissionsById(int deptId) throws Exception{
        List<OnedecDeptPermission> list = deptPermissionService.selectPermissionByDeptid(deptId);
        ArrayList<Integer> objects = new ArrayList<>();
        for (OnedecDeptPermission one:list) {
            objects.add(one.getPermissionId());
        }
        return new ResponseEntity("200","success",objects);
    }

    @RequestMapping("getRolePermissionsById")
    public ResponseEntity getPermissionsByRoleId(int roleId) throws Exception{
        List<OnedecRolePermission> list = rolePermissionService.selectPermissionByRoleid(roleId);
        ArrayList<Integer> objects = new ArrayList<>();
        for (OnedecRolePermission one:list) {
            objects.add(one.getPermissionId());
        }
        return new ResponseEntity("200","success",objects);
    }

    @PostMapping("addDept")
    @Transactional
    public ResponseEntity addDept(@RequestBody List<Menu> list,String deptName) throws Exception{
        OnedecDept onedecDept = new OnedecDept();
        onedecDept.setDeptName(deptName);
        if(deptService.selectByName(deptName).size()!=0){
            throw new RuntimeException("已存在该部门");
        }else {
            deptService.insert(onedecDept);
            int deptId = onedecDept.getDeptId();
            for (Menu menu : list) {
                OnedecDeptPermission onedecDeptPermission = new OnedecDeptPermission();
                onedecDeptPermission.setDeptId(deptId);
                OnedecPermission onedecPermission = menuService.selectById(menu.getValue());
                if (onedecPermission.getPermissionLevel() == 1) {
                    onedecDeptPermission.setPermissionId(menu.getValue());
                    deptPermissionService.insert(onedecDeptPermission);
                } else if (onedecPermission.getPermissionLevel() == 2) {
                    int parentId = onedecPermission.getPermissionParentId();

                    onedecDeptPermission.setPermissionId(menu.getValue());
                    deptPermissionService.insert(onedecDeptPermission);


                    if (deptPermissionService.selectByDeptIdAndPermissionId(deptId, parentId).size() == 0) {

                        onedecDeptPermission.setPermissionId(parentId);
                        deptPermissionService.insert(onedecDeptPermission);
                    }
                }
            }
        }
        return new ResponseEntity("200","success",null);
    }

    @PostMapping("addRole")
    @Transactional
    public ResponseEntity addRole(@RequestBody List<Menu> list,String roleName) throws Exception{
        OnedecRole onedecRole = new OnedecRole();
        onedecRole.setRoleName(roleName);
        if(roleService.selectByName(roleName).size()!=0){
            throw new RuntimeException("已存在该角色");
        }else {
            roleService.insert(onedecRole);
            int roleId = onedecRole.getRoleId();
            for (Menu menu : list) {
                OnedecRolePermission onedecRolePermission = new OnedecRolePermission();
                onedecRolePermission.setRoleId(roleId);
                OnedecPermission onedecPermission = menuService.selectById(menu.getValue());
                if (onedecPermission.getPermissionLevel() == 1) {
                    onedecRolePermission.setPermissionId(menu.getValue());
                   rolePermissionService.insert(onedecRolePermission);
                } else if (onedecPermission.getPermissionLevel() == 2) {
                    int parentId = onedecPermission.getPermissionParentId();

                    onedecRolePermission.setPermissionId(menu.getValue());
                    rolePermissionService.insert(onedecRolePermission);


                    if (rolePermissionService.selectByRoleIdAndPermissionId(roleId, parentId).size() == 0) {

                        onedecRolePermission.setPermissionId(parentId);
                        rolePermissionService.insert(onedecRolePermission);
                    }
                }
            }
        }
        return new ResponseEntity("200","success",null);
    }
}
